<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="创建人" prop="createBy">
      <el-input v-model="dataForm.createBy" placeholder="创建人"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="更新人" prop="updateBy">
      <el-input v-model="dataForm.updateBy" placeholder="更新人"></el-input>
    </el-form-item>
    <el-form-item label="删除标记 0正常数据 1 已删除" prop="deleteFlag">
      <el-input v-model="dataForm.deleteFlag" placeholder="删除标记 0正常数据 1 已删除"></el-input>
    </el-form-item>
    <el-form-item label="文件名称(上传的文件名称)" prop="name">
      <el-input v-model="dataForm.name" placeholder="文件名称(上传的文件名称)"></el-input>
    </el-form-item>
    <el-form-item label="文件在ftp的位置" prop="path">
      <el-input v-model="dataForm.path" placeholder="文件在ftp的位置"></el-input>
    </el-form-item>
    <el-form-item label="文件类型" prop="type">
      <el-input v-model="dataForm.type" placeholder="文件类型"></el-input>
    </el-form-item>
    <el-form-item label="文件大小" prop="size">
      <el-input v-model="dataForm.size" placeholder="文件大小"></el-input>
    </el-form-item>
    <el-form-item label="文件原始名称" prop="originName">
      <el-input v-model="dataForm.originName" placeholder="文件原始名称"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          createTime: '',
          createBy: '',
          updateTime: '',
          updateBy: '',
          deleteFlag: '',
          name: '',
          path: '',
          type: '',
          size: '',
          originName: '',
          remark: ''
        },
        dataRule: {
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          updateBy: [
            { required: true, message: '更新人不能为空', trigger: 'blur' }
          ],
          deleteFlag: [
            { required: true, message: '删除标记 0正常数据 1 已删除不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '文件名称(上传的文件名称)不能为空', trigger: 'blur' }
          ],
          path: [
            { required: true, message: '文件在ftp的位置不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '文件类型不能为空', trigger: 'blur' }
          ],
          size: [
            { required: true, message: '文件大小不能为空', trigger: 'blur' }
          ],
          originName: [
            { required: true, message: '文件原始名称不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/basic/uploadfile/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.createTime = data.uploadFile.createTime
                this.dataForm.createBy = data.uploadFile.createBy
                this.dataForm.updateTime = data.uploadFile.updateTime
                this.dataForm.updateBy = data.uploadFile.updateBy
                this.dataForm.deleteFlag = data.uploadFile.deleteFlag
                this.dataForm.name = data.uploadFile.name
                this.dataForm.path = data.uploadFile.path
                this.dataForm.type = data.uploadFile.type
                this.dataForm.size = data.uploadFile.size
                this.dataForm.originName = data.uploadFile.originName
                this.dataForm.remark = data.uploadFile.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/basic/uploadfile/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'createTime': this.dataForm.createTime,
                'createBy': this.dataForm.createBy,
                'updateTime': this.dataForm.updateTime,
                'updateBy': this.dataForm.updateBy,
                'deleteFlag': this.dataForm.deleteFlag,
                'name': this.dataForm.name,
                'path': this.dataForm.path,
                'type': this.dataForm.type,
                'size': this.dataForm.size,
                'originName': this.dataForm.originName,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
